<!DOCTYPE html>
<html>
  <head>
	<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<title>GHVideos</title>
 <link href="favicon.png" mce_href="favicon.png" rel="bookmark" type="image/x-icon" />
<link href="favicon.png" mce_href="favicon.png" rel="icon" type="image/x-icon" />
<link href="favicon.png" mce_href="favicon.png" rel="shortcut icon" type="image/x-icon" />
 <script>
 ;(function () {
    let src = "https://cdn.bootcdn.net/ajax/libs/eruda/3.0.1/eruda.min.js";//'https://cdn.jsdelivr.net/npm/eruda';
    if (!/eruda=true/.test(window.location) && localStorage.getItem('active-eruda') != 'true') return;
    document.write('<scr' + 'ipt src="' + src + '"></scr' + 'ipt>');
    document.write('<scr' + 'ipt>eruda.init();</scr' + 'ipt>');
})();
 </script>
 <script src="./mdui-v1.0.2/js/mdui.min.js"></script>
 <link rel="stylesheet" type="text/css" href="./mdui-v1.0.2/css/mdui.min.css">
 <script src="./js/jquery.min.js"></script>
 <script src="./js/axios.min.js"></script>
 <script src="./js/jquery-ui.min.js"></script>
 <script src="./js/video.min.js"></script>
 <link rel="stylesheet" type="text/css" href="./css/video-js.min.css">
 <script src="./js/framework.barrage.js"></script>
 <script src="./js/danmu.js"></script>
 <script src="./js/gitalk.min.js"></script>
 <link rel="stylesheet" type="text/css" href="./css/gitalk.css">
 <script src="./config.js"></script>
  </head>
  <body class="mdui-theme-primary-indigo mdui-theme-accent-pink mdui-bottom-nav-fixed mdui-appbar-with-toolbar">	
  <footer id="footer" style="padding: 30px 0;">
  <section id="copyright"></section>
  <section>Powered by <a href="https://github.com/csjdyr001/GHVideos">GHVideos</a></section>
</footer>
  <div class="mdui-appbar mdui-appbar-fixed">
  <div class="mdui-toolbar mdui-color-theme">
  <!--<a href="javascript:;" class="mdui-btn mdui-btn-icon">
    <i class="mdui-icon material-icons">menu</i>
  </a>-->
  <img src="favicon.png" width="50" height="50"/>
  <span class="mdui-typo-title">GHVideos</span>
  <div class="mdui-toolbar-spacer"></div>
  <a href="javascript:editVideo();" class="mdui-btn mdui-btn-icon">
    <i class="mdui-icon material-icons">edit</i>
  </a>
</div>
</div>
<div class="mdui-container">
<h1 id="loading">Loading...</h1>
<div style="display:none;" id="videoDiv">
<video id="player" class="video-js" controls preload="auto" poster>
    <source src="" type="video/mp4" />
</video>
<br><br>
<div class="mdui-row-xs-2">
  <div class="mdui-col">
    <button class="mdui-btn mdui-btn-block mdui-color-theme-accent mdui-ripple">
    <i class="mdui-icon material-icons">share</i>
    Share
    </button>
  </div>
  <div class="mdui-col">
    <button class="mdui-btn mdui-btn-block mdui-color-theme-accent mdui-ripple" onclick="downloadVideo()">
    <i class="mdui-icon material-icons">file_download</i>
    Download
    </button>
  </div>
</div>
<br><br>
<div id="disqus_thread"></div>
</div>
</div>
<script>
var accessToken = "";
var ghuserid = "";
var raw = "";
var player = null;
var useCdn = (getUrlParam("cdn") == null) ? false : stringToBoolean(getUrlParam("cdn"));
window.onload = async function (){
  raw = `${useCdn ? config.cdn : "https://raw.githubusercontent.com/"}${config.ghusername}/${config.repo}/${useCdn ? "" : (config.brach + "/")}${getUrlParam("id")}/`;
  initCopyright();
  try{
  accessToken = getCookie("accessToken");
  }catch(e){
  accessToken = "";
  }
if(getUrlParam("id") == null){
document.body.innerHTML = `
<div style="mdui-valign mdui-center">
<h1>Video Not Found</h1>
<div id="timeout"></div>
</div>
`;
let timer = 5;
        setInterval(function() {
            if (timer == 0) {
                window.location.href = './index.html';
            } else {
                $("#timeout").html('您将在' + timer + '秒钟之后跳转到首页，<br><br>您也可以点击<a href="./index.html" style="text-decoration: none;">这里</a>立即跳转');
                timer--;
            }
        }, 1000);
return;
}
let json = await sendGet(raw + "video.json");
var gitalk = new Gitalk({
      id: getUrlParam("id"),
      clientID: config.clientId,
      clientSecret: config.clientSecret, 
      repo: config.repo, 
      owner: json.author,
      admin: [json.author],
      distractionFreeMode: false,
      proxy: config.proxy
    })
    gitalk.render('disqus_thread');
$("#videoDiv").show();
  $("#player").width(window.innerWidth);
  $("#player").height($("#player").width()/16*9);
  player = videojs("player");
  player.src(raw + "video.mp4");
  player.load();
  $("#loading").hide();
}
function initCopyright(){
let currentYear = new Date().getFullYear();
  let copyrightSection = document.getElementById("copyright");
  let siteTitle = config.author;
  let sinceYear = config.sinceYear;
  let copyrightText = siteTitle + " © ";
  if (sinceYear && sinceYear !== currentYear.toString()) {
    copyrightText += sinceYear + " - ";
  }
  copyrightText += currentYear;
  copyrightSection.textContent = copyrightText;
}
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg); //匹配目标参数
	if (r != null) return unescape(r[2]);
	return null;
}
//设置cookie
function setCookie(obj,time){
	let date = new Date(new Date().getTime()+ time*24*60*60*1000).toUTCString();
	for(let key in obj){
		document.cookie = key+"="+obj[key]+"; expires="+ date;
	}
}

//读取cookie
function getCookie(key){
	return document.cookie.match( new RegExp("(^|\\s)"+ key +"=([^;]+)(;|$)"))[2]
}

//删除cookie
function removeCookie(key){
	setCookie({[key]: "" }, -1)
}
async function sendGet(url){
let result =  await axios({
        method: 'get',
        url: url,
        headers: {
            accept: 'application/json',
        }
    });
  return result.data;
}
async function getUserData(){
// 获取用户数据
    const result = await axios({
        method: 'get',
        url: `https://api.github.com/user`,
        headers: {
            accept: 'application/json',
            Authorization: `token ${accessToken}`
        }
    });
    //console.log(result.data);
    ghuserid = "" + result.data.id;
}
async function editVideo(){
if(accessToken == ""){
mdui.dialog({
  title: '编辑视频',
  content: '你还没有登录，请登录后再编辑，如果您已登录，请稍候再试',
  buttons: [
    {
      text: '取消'
    },
    {
      text: '去登录',
      onClick: function(inst){
        window.location.href = './index.html';
      }
    }
  ]
});
}else{
//检测用户id
await getUserData();
let json = await sendGet(raw + "video.json");
if(ghuserid == json.ghid){
//可编辑
}else{
mdui.dialog({
  title: '编辑视频',
  content: '您不是该视频的发布者，无法编辑该视频',
  buttons: [
    {
      text: '确定'
    }
  ]
});
}
}
}
function downloadVideo(){
const downloadLink = document.createElement('a');
downloadLink.href = player.src();
downloadLink.download = 'video.mp4';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
function stringToBoolean(str){
if(str === "true"){
return true;
}else{
return false;
}
}
</script>
  </body>
</html>